﻿using AutoMapper;
using MediatR;
using SupplyChain.Domain.Entities;
using SupplyChain.Infrastructure.Interface;
using SupplyChain.Write.API.Application.Command.Supplier;

namespace SupplyChain.Write.API.Application.CommandHandler.Supplier
{
    public class DelSupplierCategoryHandler : IRequestHandler<DelSupplierCategoryCommand, bool>
    {
        private readonly IBaseRepository<SupplierCategory> repository;

        public DelSupplierCategoryHandler(IBaseRepository<SupplierCategory> repository)
        {
            this.repository = repository;
        }

        public async Task<bool> Handle(DelSupplierCategoryCommand request, CancellationToken cancellationToken)
        {
            if (request.Id == 0) return await Task.FromResult(false);

            SupplierCategory category = await repository.GetFirstOrDefaultAsync(x => !x.IsDel && x.Id == request.Id);
            category.IsDel = !category.IsDel;
            return await Task.FromResult(await repository.UpdateAsync(category));
        }
    }
}
